<?php
/**
 * --------------------------------------------------------------
 * 首页控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\index\controller;

use app\common\controller\HomeBase;
use app\admin\model\Advert as AdvertModel;
use app\admin\model\ManualTypes as ManualTypesModel;
use app\admin\model\Manual as ManualModel;
use app\admin\model\Document as DocumentModel;
use app\admin\model\Column as ColumnModel;

class Index extends HomeBase
{
    public function index()
    {
        // 读取焦点广告
        $adver_where_one = [
            ['name', '=', 'index_focus_pic'],
            ['timeset', '=', 0],
            ['status', '=', 1]
        ];
        $adver_where_two = [
            ['name', '=', 'index_focus_pic'],
            ['timeset', '=', 1],
            ['start_time', '<=', time()],
            ['end_time', '>=', time()],
            ['status', '=', 1]
        ];
        $advert_list = AdvertModel::whereOr([$adver_where_one, $adver_where_two])->field('id,content')->select();
        $advert_list = $advert_list ? object_to_array($advert_list) : null;
        if ($advert_list) {
            foreach ($advert_list as $k => &$v) {
                $advertArr    = json_decode($v['content'], true);
                $v['link']    = $advertArr['link'];
                $v['pic']     = get_file_path($advertArr['one_pic']);
                $v['width']   = $advertArr['width'];
                $v['height']  = $advertArr['height'];
                $v['alt']     = $advertArr['alt'];
                unset($v['content']);
            }
        }

        // 手册类型
        $manual_types = ManualTypesModel::where(['pid' => 0, 'status' => 1])
                        ->order('id desc,sort asc')
                        ->field('id,title')
                        ->select();

        // 最新手册
        $manual_list = ManualModel::where(['status' => 1])
                       ->field('id,title,route_name,cover')
                       ->order('id desc,sort asc')
                       ->limit(12)
                       ->select();
        
        // 最新文章
        $article_list = DocumentModel::view('document', 'id,title')
                        ->view("column", 'title as column_title,id as column_id,route_name as column_route_name', 'column.id = document.cid', 'left')
                        ->where(['document.status' => 1])
                        ->order('document.create_time desc')
                        ->limit(24)
                        ->select();
        $article_list = $article_list ? object_to_array($article_list) : null;
        
        if ($article_list) foreach ($article_list as $k => &$v) {
            if (empty($v['column_route_name'])) {
                $v['column_url'] = url('/article/list_'.$v['column_id']);
            } else {
                $v['column_url'] = url('/article/'.$v['column_route_name']);
            }
        }
		
		// 文章分类，这里只读取一级分类
		$article_types = ColumnModel::where(['pid' => 0, 'status' => 1])
                        ->order('id desc,sort asc')
                        ->field('id,title,route_name')
                        ->select();
        $article_types = $article_types ? object_to_array($article_types) : null;
        
        if ($article_types) foreach ($article_types as $key => &$val) {
            if (empty($val['route_name'])) {
                $val['column_url'] = url('/article/list_'.$val['column_id']);
            } else {
                $val['column_url'] = url('/article/'.$val['route_name']);
            }
        }
        
        $this->assign([
            'title'         => config('app.site_setting.web_site_title'),
            'advert_list'   => $advert_list,
            'manual_types'  => $manual_types,
            'article_types' => $article_types,
            'manual_list'   => $manual_list,
            'article_list'  => $article_list
        ]);
        return $this->fetch();
    }
}
